package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.dao.IStrategyCommentDAO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private IStrategyCommentDAO strategyCommentDAO;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(StrategyComment comment) {
        if(comment.getId()==null){
            comment.setCreateTime(new Date());
        }
        strategyCommentDAO.save(comment);
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {

        /*Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), sort);

        return strategyCommentDAO.findByDetailId(qo.getDetailId(),  pageable);*/
        Criteria criteria = null;
        if (qo.getDetailId() != -1) {
            criteria = Criteria.where("detailId").is(qo.getDetailId());
        } else {
            criteria = Criteria.where("detailId").ne(qo.getDetailId());
        }
        if (qo.getKeyword() != null) {
            criteria.and("content").regex("^.*" + qo.getKeyword() + ".*$");
        }
        //查询封装条数
        Query query = new Query();

        //添加查询条件
        query.addCriteria(criteria);

        //查询总条数
        long count = mongoTemplate.count(query, StrategyComment.class);

        if (count == 0) {
            return Page.empty();
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        query.with(pageable);

        //添加排序规则
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        query.with(sort);

        //查询所有记录数
        List<StrategyComment> list = mongoTemplate.find(query, StrategyComment.class);

        return new PageImpl<StrategyComment>(list, pageable, count);
    }


    public boolean thumbUp(String toid, Long fromid) {

        Optional<StrategyComment> optional = strategyCommentDAO.findById(toid);

        StrategyComment comment = optional.get();

        List<Long> thumbuplist = comment.getThumbuplist();
        //点赞
        boolean flag = false;
        if (!thumbuplist.contains(fromid)) {
            comment.setThumbupnum(comment.getThumbupnum() + 1);
            thumbuplist.add(fromid);
            flag = true;
        }else{
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            thumbuplist.remove(fromid);
        }
        comment.setThumbuplist(thumbuplist);
        this.save(comment);
        return flag;
    }


}
